package com.app.project;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class BESM_CheckOnline extends Activity{
	
	private Handler mHandler = new Handler();
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		
//		mHandler.removeCallbacks(UpdateData);
//		mHandler.postDelayed(UpdateData, 100);
	}
	
	public boolean isConnectInternet(){
        ConnectivityManager connect = (ConnectivityManager) 
                getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connect != null){
            NetworkInfo[] info = connect.getAllNetworkInfo();
            if (info != null) 
                for (int i = 0; i < info.length; i++) 
                    if(info[i].getState() == NetworkInfo.State.CONNECTED){
                        return true;
                    }
            }
          return false;
    }
	
//	private Runnable UpdateData = new Runnable() {
//
//		public void run() {
//			if(isConnectInternet()){
//				
//			}else{
//				mHandler.removeCallbacks(UpdateData);
//			}
//		
//
//		}
//	};
	public boolean networkSignOn() {
	    HttpURLConnection urlConnection = null;
	    try {
	        URL url = new URL("http://clients3.google.com/generate_204");
	        urlConnection = (HttpURLConnection) url.openConnection();
	        urlConnection.setInstanceFollowRedirects(false);
	        urlConnection.setConnectTimeout(5000);
	        urlConnection.setReadTimeout(5000);
	        urlConnection.setUseCaches(false);
	        urlConnection.getInputStream();
	        return urlConnection.getResponseCode() == 204;
	    } catch (IOException e) {
	        Log.v("Walled garden check - probably not a portal: exception " + e, "");
	        return false;
	    } finally {
	        if (urlConnection != null) urlConnection.disconnect();
	    }
	}
}
